import React, { Component } from 'react';
import {qrCode,accQrCode} from '../../Action/url'
import wx from 'weixin-js-sdk';
import {loadToken,ROOT_URL,ROOT_WEB_URL,SCREEN_WIDTH} from '../../Action/rpc'
import {imei,version,client,MyInfo} from '../../Action/auth'
import {initWxShare} from '../../Action/wxUtil'

export default class ErCode extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            path:'',
            personalId:'',
            name:''
        };
    }

    async componentWillMount() {
        var u = navigator.userAgent;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if(isiOS){
            if(sessionStorage.getItem('ios')){
                const token = await loadToken()
                const erweiCodePath = ROOT_URL+qrCode +'?'
                    +'token='+token+'&client='+'wx'+'&imei='+imei+'&version='+version+'&width=180&height=180'
                this.setState({path:erweiCodePath})
                await this.getMyInfo()

                initWxShare(
                    "维魅力微商邀请授权",
                    ROOT_WEB_URL+"authorization?recommendId="+this.state.personalId,
                    "../images/logo.png",
                    "",
                    "",
                    "",
                    0,
                    this.state.personalId
                )
            }else{
                sessionStorage.setItem('ios',true)
                window.location.reload()
            }
        }else{
            const token = await loadToken()
            const erweiCodePath = ROOT_URL+qrCode +'?'
                +'token='+token+'&client='+'wx'+'&imei='+imei+'&version='+version+'&width=180&height=180'
            this.setState({path:erweiCodePath})
            await this.getMyInfo()

            initWxShare(
                "维魅力微商邀请授权",
                ROOT_WEB_URL+"authorization?recommendId="+this.state.personalId,
                "http://shyt-image.oss-cn-shanghai.aliyuncs.com/logo.png",
                "",
                "",
                "",
                0,
                this.state.personalId
            )
        }
        this.getMyInfo();
    }

    async getMyInfo(){
        await MyInfo()
            .then(res=>{
                this.setState({personalId:res.ID,name:res.MEMBER_NAME})
            })
    }

    render() {
        return (
            <div className="containerNav">
                <div style={{width:'100%',height:(SCREEN_WIDTH*73)/75}}>
                    <img src={require('../../Images/common/erCodeBkImg.png')} alt=""/>
                </div>
                <div className="di pa flex flex-pack-center"
                     style={{width:'100%',height:'6.5rem',top:(SCREEN_WIDTH*73)/75-66,zIndex:100}}>
                    <div className="erCodebk flex flex-pack-center flex-align-center erbox_showdow"
                         style={{width:'6.5rem',borderRadius:10,padding:8}}>
                        <span className="di bgk_fff width100 height100" style={{padding:8}}>
                            <img src={this.state.path} alt=""/>
                        </span>
                    </div>
                </div>
                <div className="posr" style={{width:'100%',height:(SCREEN_WIDTH*16)/25}}>
                    <div className="pa width100 tc f18" style={{top:'3.7rem'}}>我是&nbsp;&nbsp;
                        <span className="colorlv">{this.state.name}</span>
                    </div>
                    <img src={require('../../Images/common/erCodebt.png')} alt=""/>
                </div>
            </div>
        );
    }
}
